<?php
namespace App\Http\Controllers\Web\V1;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Exceptions\WebExceptions\WebException;
use App\Service\Activity\ActivityService;
use App\Model\Activity;
use App\Lib\ImageUploadHandler;
use App\Service\PrizeService;

class ActivityController extends BaseController
{
	protected $returnData = ['errCode' => 0,'errMsg' => '','data' => []];

	public function __construct(ActivityService $activityService)
	{	
		$this->service = $activityService;
	}
	
	/**
	 * 活动列表页
	 * @return [type] [description]
	 */
	public function list()
	{
		$result = $this->service->withAll(['prize','sponsors'])->getAllList();
		$this->returnData['data'] = $result->toArray();
		return $this->returnData;
	}

	/**
	 * 活动详情
	 * @return [type] [description]
	 */
	public function detail($id)
	{
		$result = $this->service->withAll(['prize','sponsors'])->getRow($id);
		if ($result) {
			$data = $result->toArray();
			$this->returnData['data'] = $data;
		}
		return $this->returnData;
	}

	/**
	 * [save description]
	 * @return [type] [description]
	 */
	public function save(Request $request)
	{
		$input = $request->input();
		$id = $input['id'] ?? 0;
		//验证规则
		$rules = [
			'title'      => 'required|max:20|unique:activity,title',
			'start_at'   => 'required',
			'end_at'     => 'required|after:start_at',
			'prize_ids'  => 'required',
			'imgs_ids'   => 'required',
			'sponsor_id' => 'required',
			'prize_desc' => 'required',
		];
		//验证提示信息
		$messages = [
			'title.required'      => '活动名称不能为空',
			'titil.max'           => '请输入活动名称在20字以内',
			'title.unique'		  => '活动名称已存在',
			'start_at.required'   => '请设置活动开始时间',
			'end_at.required'     => '请设置活动结束时间',
			'end_at.after'        => '活动结束时间不能小于开始时间',
			'prize_ids.required'  => '请设置活动奖品',
			'imgs_ids.required'   => '请上传活动图片',
			'sponsor_id.required' => '请选择赞助商',
			'prize_desc.required' => '请输入活动奖品介绍'
		];
		//提交验证
		$validator = Validator::make($input,$rules,$messages);
		if ($validator->fails()) {
			throw new WebException($validator->errors()->first());
		}

		/*$now = time();
    	//活动未开始
    	if ($input['start_at'] > $now) {
    		$input['status'] = 0;
    	//活动预热
    	}else if ($now < $input['start_at'] && ($now + 3*3600) > $input['start_at']) {
    		$input['status'] = 1;
    	//活动进行中
    	}else if($input['start_at'] < $now  &&  $now < $input['end_at']) {
    		$input['status'] = 2;
    	}else { //活动结束
    		$input['status'] = 3;
    	}*/
    	$prize_ids = explode(',',$input['prize_ids']);
		if ($id) {
			$where['activity_id'] = [$id];
			(new PrizeService())->updateBatch($where,['activity_id' => 0]);
			$res = $this->service->saveRow($id,$input);
			(new PrizeService())->updateBatch(['id' => $prize_ids],['activity_id' => $id]);
		}else {
			$where['id'] = $prize_ids;
			//添加记录
			$res = $this->service->add($input);
			(new PrizeService())->updateBatch($where,['activity_id' => $res->id]);
		}
		if ($res) {
			success();
		}
	}

	/**
	 * 活动结束接口
	 * @return [type] [description]
	 */
	public function activityClose(Request $request)
	{
		$id = $request->input('id');
		$result = $this->service->getRow($id);
		if (empty($result['data'])) {
			throw new WebException('该活动不存在或已被删除','-1');
		}
		$res= $this->service->saveRow($id,['status' => 3]);
		if ($res) {
			success('活动已结束');
		}
		throw new WebException('结束活动失败','-2');
	}

	/**
	 * 活动删除
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function activityDel(Request $request)
	{
		$id = $request->input('id');
		$result = $this->service->getRow($id);
		if (empty($result['data'])) {
			throw new WebException('该活动不存在或已被删除','-1');
		}
		$res = $this->service->del($id);
		if ($res) {
			success('删除成功');
		}
		throw new WebException('删除失败','-2');
	}

	/**
	 * 上传图片到cnd
	 * @param ImageUploadHandler $upload [description]
	 */
	public function UploadCdnIamge(ImageUploadHandler $upload)
	{
		$data = $upload->saveCdnImage('activity');
		$this->returnData['data'] = $data;

		return $this->returnData;
	}
}